<template>
    <div class="nva_bg">
      <div class="center">
          <el-row>
            <el-col :span="20">
              <ul>
                <li><RouterLink to="/">商城首页</RouterLink></li>
                <li><RouterLink to="/">商城首页</RouterLink></li>
                <li><RouterLink to="/">商城首页</RouterLink></li>
                <li><RouterLink to="/">商城首页</RouterLink></li>
                <li><RouterLink to="/">商城首页</RouterLink></li>
                <li><RouterLink to="/">商城首页</RouterLink></li>
                <li><RouterLink to="/">商城首页</RouterLink></li>
              </ul>
            </el-col>
            <el-col :span="4">
              <ul class="user">
                <template v-if="!userStore.userInfo">
                  <li><RouterLink to="/user/login">登录</RouterLink></li>
                  <li><RouterLink to="/user/reg">注册</RouterLink></li>
                </template>
                <template v-else>
                  <lli><RouterLink to="/user/cart"><el-icon><ShoppingCart/></el-icon></RouterLink></lli>
                  <li><RouterLink to="/user/center">{{ userStore.userInfo.username }}</RouterLink></li>
                  <li><el-link :underline="false" @click="logout">退出</el-link></li>
                </template>

              </ul>
            </el-col>
          </el-row>
      </div>
    </div>
    <!--头部搜索-->
    <div class="center">
      <el-row>
        <el-col :span="16">
          <img src="/src/assets/logo.cn.png" style="height: 90px;" alt="">
        </el-col>
        <el-col :span="8">
          <div class="search">
            <!--搜索按钮-->
            <el-button class="search_btn" @click="search"><el-icon><Search/></el-icon></el-button>
            <!--搜索框-->
            <el-input type="text" class="search_input" v-model="goodsName"></el-input>
          </div>
        </el-col>
      </el-row>
    </div>
    <!--上架的第一级分类-->
    <div class="center">
      <ul class="category">
        <li v-for="(category, index) in parentList" :key="index"><RouterLink :to="`/user/search${category.id}`">{{category.name}}</RouterLink></li>
      </ul>
    </div>
</template>

<script setup>
import {Search} from "@element-plus/icons-vue";
import categoryApi from "@/api/categoryApi.js";
import {ref} from "vue";
import {useUserStore} from "@/stores/user.js";
import {useTokenStore} from "@/stores/tokenStore.js";
import {useRouter} from "vue-router";

const router = useRouter();
const tokenStore = useTokenStore();
const userStore = useUserStore();
const parentList = ref([]);
//1.获取父分类 2.获取上架的
const getParent = ()=>{
    const condition = {
      parentId: 0,
      status: 1
    };
    categoryApi.selectByPage(condition)
        .then(resp => {
          parentList.value = resp.data;
        })
}
const goodsName = ref('');
//点击搜索跳转Search界面
const search = ()=>{
  //携带goodsName跳到search页面
  router.push({
    path: '/user/search',
    query: {
      goodsName: goodsName.value
    }
  })
}
const logout = () => {
  //清空token
  tokenStore.$reset();
  //清空用户信息
  userStore.$reset();
  //跳转到前台首页
  router.push("/user/index")
}
getParent();
</script>

<style scoped>
  .user .el-icon{
    top: 5px;
  }
  /*头部导航*/
  .nva_bg{
    background-color: var(--theme-bg-color);
    line-height: 40px;
  }
  .nva_bg ul li {
    float: left;
    margin-right: 20px;
  }
  .user li {
    float: right;
  }
  .user {
    float: right;
  }
  a {
    color: #999;
  }
  a:hover {
    color: #fff;
  }

  /*搜索框样式*/
  .search {
    padding-top: 20px;
  }
  .search_input {
    width: 260px;
    float: right;
  }
  .search_btn{
    float: right;
    color: #fff;
    margin-left: 5px;
    border-radius: 0px;
    border: 0px;
    background-color: var(--theme-color);
  }
  .search_btn:hover {
    background-color: wheat ;
  }

  /*一级分类样式*/
  .category {
    background-color: var(--theme-color);
    line-height: 30px;
  }
  .category li {
    float: left;
    padding: 0 10px;
  }
  .category li a {
    color: #fff;
  }
  .category li:hover {
    background-color: wheat;
  }
</style>